import qs from 'qs';

import axios from 'axios'
const baseConfig = {
	headers: {
		'Content-Type': ' application/json'
	}
}
/**
 * 公共接口地址
 */
let baseUrl = 'http://192.168.1.158/maopiao/public/api/wxapp'
export default class HttpUtil {
	constructor(config) {
		this._http = this._createHttp(Object.assign(baseConfig, config))
	}

	get(url, params = {}) {
		return this._http.get(baseUrl + url, {
			params
		}).then(this._handleResult)
	}

	post(url, params = {}) {
		return this._http.post(baseUrl + url, params).then(this._handleResult)
	}
	post2(params) {
		return this._http.post('http://192.168.1.158/maopiao/public/api/wxapp/upload/uploadImage',{'image':'w.jpg',"mc":'1',type:"image"},{headers:{'Content-Type':'application/x-www-form-urlencoded'}}).then(this._handleResult)
	}

	_createHttp(config) {
		return axios.create(config)
	}

	_handleResult(res) {
		return res.data;
	}
}

function get(url, params) {
	return new Promise((resolve, reject) => {
		axios.get(url, {
			params: params
		}).then(res => {
			resolve(res.data);
		}).catch(err => {
			reject(err.data)
		})
	});
}

function post(url, params) {
	return new Promise((resolve, reject) => {
		axios.post(url, {
			params: params
		}).then(res => {
			resolve(res.data);
		}).catch(err => {
			reject(err.data)
		})
	});
}
// export default {
//   post: post,
//   get: get
// }
